/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-29
*/
package com.rzico.order.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Product;
import com.rzico.order.entity.Cart;
import com.rzico.order.entity.CartItem;
import com.rzico.util.RedisHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.order.mapper.CartMapper;

import java.util.*;

/**
 * <pre>
 * 购物车业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class CartService extends BaseServiceImpl<Cart, String> {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private CartItemService cartItemService;
    @Autowired
    private RedisHandler redisHandler;

    @Override
    public BaseMapper<Cart, String> getMapper() {
        return cartMapper;
    }


    public Cart getCurrent(Member member,String uuid) {
        String key = uuid;
        if (member!=null) {
            key = "cart-"+String.valueOf(member.getId());
        }
        String value = key;
        if (key!=null) {
            while (!redisHandler.lock(key, value)) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            Cart cart = null;
            if (member != null) {
                List<Cart> ucartList = new ArrayList<>();
                if (uuid != null) {
                    Map<String, Object> uparams = new HashMap<>();
                    uparams.put("cartKey", uuid);
                    uparams.put("sortField", "id");
                    uparams.put("sortType", "asc");
                    ucartList = selectList(uparams);
                }
                //没登录前的购物车要合并到会员购物车
                if (ucartList.size() > 0) {
                    cart = ucartList.get(0);
                    if (cart.getMemberId()==null) {
                        cart = this.merge(member, ucartList.get(0));
                    } else {
                        Map<String, Object> params = new HashMap<>();
                        params.put("memberId", member.getId());
                        params.put("sortField", "id");
                        params.put("sortType", "asc");
                        List<Cart> cartList = selectList(params);
                        if (cartList.size() > 0) {
                            cart = cartList.get(0);
                        } else {
                            cart = null;
                        }
                    }
                } else {
                    Map<String, Object> params = new HashMap<>();
                    params.put("memberId", member.getId());
                    params.put("sortField", "id");
                    params.put("sortType", "asc");
                    List<Cart> cartList = selectList(params);
                    if (cartList.size() > 0) {
                        cart = cartList.get(0);
                    } else {
                        cart = null;
                    }
                }

            } else {
                Map<String, Object> params = new HashMap<>();
                params.put("cartKey", uuid);
                params.put("sortField", "id");
                params.put("sortType", "asc");
                List<Cart> cartList = selectList(params);
                if (cartList.size() > 0) {
                    cart = cartList.get(0);
                } else {
                    cart = null;
                }
            }
            return cart;
        } finally {
            if (key!=null) {
                redisHandler.unlock(key, value);
            }
        }

    }

    public Cart create(Member member,String uuid) {
        Cart cart = new Cart();
        if (member!=null) {
            cart.setMemberId(member.getId());
        }
        cart.setCartKey(uuid);
        cart.setCreateDate(new Date());
        super.insertUseGeneratedKeys(cart);
        return cart;
    }

    public Cart merge(Member member, Cart cart) {
        if (member != null && cart != null) {
            Cart memberCart = null;
            Map<String,Object> params = new HashMap<>();
            params.put("memberId",member.getId());
            params.put("sortField","id");
            params.put("sortType","asc");
            List<Cart> cartList = selectList(params);
            if (cartList.size()>0) {
                memberCart = cartList.get(0);
            }
            if (memberCart != null && !memberCart.getId().equals(cart.getId())) {
                memberCart.setItemList(cartItemService.getItemList(memberCart.getId()));
                for (CartItem cartItem:memberCart.getItemList()) {
                    cartItem.setChk(false);
                    cartItemService.updateByPrimaryKeySelective(cartItem);
                }
                for (CartItem iterator:cartItemService.getItemList(cart.getId())) {
                    CartItem item = null;
                    for (CartItem cartItem:memberCart.getItemList()) {
                        if (cartItem.getProductId().equals(iterator.getProductId())) {
                            item = cartItem;
                            break;
                        }
                    }
                    if (item!=null) {
                        item.setChk(iterator.getChk());
                        item.setQuantity(item.getQuantity()+iterator.getQuantity());
                        cartItemService.updateByPrimaryKeySelective(item);
                    } else {
                        iterator.setCartId(memberCart.getId());
                        cartItemService.updateByPrimaryKeySelective(iterator);
                    }
                }
                this.clear(cart);
                return memberCart;
            } else {
                cart.setMemberId(member.getId());
                super.updateByPrimaryKeySelective(cart);
                return cart;
            }
        }

        return  null;


    }

    public void clear(Cart cart) {
        if (cart==null) {
            return;
        }
        CartItem cartItem = new CartItem();
        cartItem.setCartId(cart.getId());
        cartItemService.delete(cartItem);
        if (cart.getMemberId()==null) {
            cartMapper.deleteByPrimaryKey(cart.getId());
        }
    }

}
